package com.zm.dao;

import com.zm.domain.Usermy;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
//开启二级缓存
//与xml开发的不同的是，不需要再在查询的方法处配置useCache="true"开启二级缓存
@CacheNamespace(blocking = true)
public interface IUserDao {
    /**
     * 查询所有用户
     * @return
     */
    @Select(value = "select * from usermy")
    //pojo类一对多映射规则的注解
    @Results(id = "userMapToMany",value = {
            @Result(id = true,column = "id",property = "userId"),
            @Result(column = "username",property = "userName"),
            @Result(column = "birthday",property = "userBirthday"),
            @Result(column = "sex",property = "userSex"),
            @Result(column = "address",property = "userAddress"),
            //一对多的映射配置
            @Result(column = "id",property = "userAccountList",many = @Many(select = "com.zm.dao.IAccountDao.findByUid",
                    fetchType = FetchType.LAZY))
         }
    )
    public List<Usermy> selectAll();

    /**
     * 根据id查询y用户
     * @param id
     * @return
     */
    @Select("select * from usermy where id=#{id}")
    @Results(id = "userMap",value = {
            @Result(id = true, column = "id", property = "userId"),
            @Result(column = "username", property = "userName"),
            @Result(column = "birthday", property = "userBirthday"),
            @Result(column = "sex", property = "userSex"),
            @Result(column = "address", property = "userAddress"),
    }
    )
    public Usermy findById(Integer id);

    @ResultMap(value = {"userMap"})
    public List<Usermy> findByCondition(Usermy usermy);
}
